package org.hackystat.iw.projectoverviewer.xml;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import org.hackystat.iw.projectoverviewer.jaxb.projects.Projects;
import org.junit.Before;
import org.junit.Test;

/**
 * Test for the XML converter.
 * 
 * @author Jianfei Liao
 */
public class TestXmlConverter {
  /** Test object. */
  private Projects projects;
  /** Test file. */
  private File file;
  /** Test package name. */
  private String packageName = "org.hackystat.iw.projectoverviewer.jaxb.projects";
  
  /**
   * Initialization before every test.
   */
  @Before
  public void initialize() {
    this.projects = new Projects();
    String userHome = System.getProperty("user.home");
    this.file = new File(userHome + "/.hackystat/projectoverviewer/test-file.xml");
    
    // creating this instance and calling toString method to 
    // reach complete coverage and stop findbugs complain
    XmlConverter converter = new XmlConverter();
    converter.toString();
  }
  
  /**
   * Tests the object to xml conversion.
   */
  @Test
  public void testObjectToXml() {
    try {
      // delete the old file if it exists
      if (file.exists()) {
        file.delete();
      }
      file.createNewFile();
      
      // check if the file size is 0
      long fileSize = file.length();
      assertEquals("New file size is 0", fileSize, 0);
      
      // convert object to xml
      boolean isSucceeded = XmlConverter.objectToXml(projects, file, packageName);
      assertTrue("Conversion is succeeded", isSucceeded);
      
      // check if the file size is greater than 0
      fileSize = file.length();
      assertTrue("New file size is greater than 0", fileSize > 0);
      
      // test exception case
      boolean notSucceeded = XmlConverter.objectToXml(projects, file, "InvalidPackage");
      assertFalse("Conversion is NOT succeeded due to JAXB exception", notSucceeded);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
  
  /**
   * Tests the xml to object conversion.
   */
  @Test
  public void testXmlToObject() {
    // check if file size is greater than 0
    long fileSize = file.length();
    assertTrue("New file size is greater than 0", fileSize > 0);
    
    // convert xml to object
    Projects testProjects = (Projects) XmlConverter.xmlToObject(file, packageName);
    
    // the projects object contains no project objects
    int projectNumber = testProjects.getProject().size();
    assertEquals("Projects object contain no project objects", 0, projectNumber);
    
    // test exception case
    testProjects = (Projects) XmlConverter.xmlToObject(file, "InvalidPackage");
    assertNull("Object is null", testProjects);
    
    // clean up
    if (this.file.exists()) {
      file.delete();
    }
  }
}
